<template>
  <div class="profile-container">
    <!-- 头部卡片 -->
    <div class="profile-header">
      <div class="header-background"></div>
      <div class="header-content">
        <div class="avatar-container">
          <img :src="userInfo.avatar" alt="用户头像" class="avatar">
          <div class="avatar-badge">
            <span class="online-status"></span>
          </div>
        </div>
        <div class="user-info">
        <div class="user-name-section">
          <h1>{{ userInfo.username }}</h1>
          <span class="user-badge">网易云音乐用户</span>
        </div>
        <p class="email">{{ userInfo.email || '暂无邮箱信息' }}</p>
        <p class="login-time">上次登录时间: {{ formatDate(new Date()) }}</p>
      </div>
      <div class="profile-actions">
        <button class="edit-profile-btn" @click="handleEditProfile">编辑资料</button>
        <router-link to="/recharge" class="recharge-btn">账户充值</router-link>
      </div>
      </div>
    </div>
    
    <!-- 用户统计数据 -->
    <div class="user-stats">
      <div class="stat-item">
        <div class="stat-number">{{ stats.playCount }}</div>
        <div class="stat-label">播放次数</div>
      </div>
      <div class="stat-item">
        <div class="stat-number">{{ stats.favoriteSongs }}</div>
        <div class="stat-label">收藏歌曲</div>
      </div>
      <div class="stat-item">
        <div class="stat-number">{{ stats.playlists }}</div>
        <div class="stat-label">创建歌单</div>
      </div>
      <div class="stat-item">
        <div class="stat-number">{{ stats.following }}</div>
        <div class="stat-label">关注</div>
      </div>
    </div>
    
    <div class="profile-content">
        <!-- 两列容器 -->
        <div class="profile-columns">
          <!-- 个人信息卡片 -->
          <div class="profile-section">
            <div class="section-header">
              <h2>个人信息</h2>
              <span class="section-icon">👤</span>
            </div>
            <div class="info-grid">
              <div class="info-item">
                <span class="label">用户名:</span>
                <span class="value">{{ userInfo.username }}</span>
              </div>
              <div class="info-item">
                <span class="label">用户ID:</span>
                <span class="value">{{ userInfo.userId }}</span>
              </div>
              <div class="info-item">
                <span class="label">注册时间:</span>
                <span class="value">{{ formatDate(userInfo.registerDate) }}</span>
              </div>
              <div class="info-item">
                <span class="label">账户状态:</span>
                <span class="value status-active">正常</span>
              </div>
              <div class="info-item">
                <span class="label">会员等级:</span>
                <span class="value">{{ userInfo.vipLevel || '普通用户' }}</span>
              </div>
              <div class="info-item">
                <span class="label">所在地区:</span>
                <span class="value">{{ userInfo.location || '未设置' }}</span>
              </div>
              <div class="info-item" v-if="userInfo.bio">
                <span class="label">个人简介:</span>
                <span class="value bio">{{ userInfo.bio }}</span>
              </div>
            </div>
          </div>
          
          <!-- 账户安全卡片 -->
          <div class="profile-section">
            <div class="section-header">
              <h2>账户安全</h2>
              <span class="section-icon">🔒</span>
            </div>
            <div class="security-info">
              <div class="security-item">
                <div class="security-left">
                  <span class="security-label">密码强度</span>
                  <span class="security-desc">上次修改于30天前</span>
                </div>
                <span class="password-strength strong">强</span>
              </div>
              <div class="security-item">
                <div class="security-left">
                  <span class="security-label">邮箱验证</span>
                  <span class="security-desc">已验证</span>
                </div>
                <span class="status-verified">✓</span>
              </div>
              <div class="security-item">
                <div class="security-left">
                  <span class="security-label">手机绑定</span>
                  <span class="security-desc">未绑定</span>
                </div>
                <span class="status-unverified">未绑定</span>
              </div>
              <div class="security-actions">
                <button class="change-password-btn">修改密码</button>
                <button class="logout-btn" @click="handleLogout">退出登录</button>
                <button class="home-btn" @click="goToProfile">个人主页</button>
              </div>
            </div>
          </div>
        </div>
        
        <!-- 最近活动卡片 -->
        <div class="profile-section recent-activities-container">
          <div class="section-header">
            <h2>最近活动</h2>
            <span class="section-icon">📝</span>
          </div>
          <div class="recent-activities">
            <div v-for="activity in recentActivities" :key="activity.id" class="activity-item">
              <div class="activity-icon">{{ activity.icon }}</div>
              <div class="activity-content">
                <div class="activity-text">{{ activity.text }}</div>
                <div class="activity-time">{{ activity.time }}</div>
              </div>
            </div>
          </div>
        </div>
      </div>
  </div>
  
  <!-- 编辑资料模态框 -->
  <EditProfileModal
    :visible="isEditModalVisible"
    :user-info="userInfo"
    @close="handleCloseEditModal"
    @update="handleProfileUpdate"
  />
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import EditProfileModal from '../components/EditProfileModal.vue';

const router = useRouter();

// 用户信息
const userInfo = ref({
  username: 'zhengbofu',
  userId: '10001',
  email: 'zhengbofu@example.com',
  registerDate: new Date('2023-01-15'),
  vipLevel: '普通用户',
  location: '北京市',
  bio: '',
  avatar: 'https://picsum.photos/seed/useravatar/150/150'
});

// 编辑模态框显示状态
const isEditModalVisible = ref(false);

// 用户统计数据
const stats = ref({
  playCount: '3,582',
  favoriteSongs: '126',
  playlists: '5',
  following: '28'
});

// 最近活动数据
const recentActivities = ref([
  {
    id: 1,
    icon: '🎵',
    text: '播放了歌曲《夏天的风》',
    time: '今天 14:30'
  },
  {
    id: 2,
    icon: '❤️',
    text: '收藏了歌曲《你的答案》',
    time: '昨天 19:45'
  },
  {
    id: 3,
    icon: '🎧',
    text: '创建了歌单《我的私藏》',
    time: '3天前'
  },
  {
    id: 4,
    icon: '👍',
    text: '点赞了歌曲《起风了》',
    time: '1周前'
  }
]);

// 格式化日期
const formatDate = (date) => {
  if (!date) return '';
  const d = new Date(date);
  const year = d.getFullYear();
  const month = String(d.getMonth() + 1).padStart(2, '0');
  const day = String(d.getDate()).padStart(2, '0');
  const hours = String(d.getHours()).padStart(2, '0');
  const minutes = String(d.getMinutes()).padStart(2, '0');
  return `${year}-${month}-${day} ${hours}:${minutes}`;
};

// 跳转到个人主页
const goToProfile = () => {
  router.push('/profile');
};

// 处理退出登录
const handleLogout = () => {
  // 清除登录状态
  localStorage.removeItem('isLoggedIn');
  localStorage.removeItem('username');
  
  // 重定向到登录页面
  router.push('/login');
};

// 处理编辑资料
const handleEditProfile = () => {
  isEditModalVisible.value = true;
};

// 处理资料更新
const handleProfileUpdate = (updatedInfo) => {
  // 更新用户信息
  Object.assign(userInfo.value, updatedInfo);
  
  // 这里可以添加保存到localStorage或API调用的逻辑
  localStorage.setItem('userInfo', JSON.stringify(userInfo.value));
  
  // 显示成功提示
  alert('资料更新成功！');
};

// 关闭编辑模态框
const handleCloseEditModal = () => {
  isEditModalVisible.value = false;
};

// 组件挂载时检查登录状态
onMounted(() => {
  const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true';
  if (!isLoggedIn) {
    router.push('/login');
  }
});
</script>

<style scoped>
.profile-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4eaf0 100%);
  padding: 0;
  display: flex;
  flex-direction: column;
}

/* 头部样式 */
.profile-header {
  position: relative;
  width: 100%;
  background-color: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.header-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #e53e3e 0%, #c53030 100%);
  opacity: 0.05;
  z-index: 1;
}

.header-content {
  position: relative;
  display: flex;
  align-items: center;
  padding: 40px;
  max-width: 1200px;
  margin: 0 auto;
  z-index: 2;
}

.avatar-container {
  position: relative;
  margin-right: 30px;
  flex-shrink: 0;
}

.avatar {
  width: 150px;
  height: 150px;
  border-radius: 50%;
  border: 4px solid #fff;
  object-fit: cover;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.avatar:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}

.avatar-badge {
  position: absolute;
  bottom: 10px;
  right: 10px;
  width: 24px;
  height: 24px;
  background-color: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.online-status {
  width: 16px;
  height: 16px;
  background-color: #4CAF50;
  border-radius: 50%;
}

.user-info {
  flex: 1;
}

.user-name-section {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 10px;
}

.user-info h1 {
  color: #333;
  font-size: 32px;
  font-weight: 700;
  margin: 0;
}

.user-badge {
  background-color: rgba(229, 62, 62, 0.1);
  color: #e53e3e;
  padding: 4px 12px;
  border-radius: 16px;
  font-size: 14px;
  font-weight: 500;
}

.user-info .email {
  color: #666;
  font-size: 16px;
  margin-bottom: 8px;
}

.user-info .login-time {
  color: #999;
  font-size: 14px;
}

.edit-profile-btn {
  background: linear-gradient(135deg, #e53e3e 0%, #c53030 100%);
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 24px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(229, 62, 62, 0.3);
}

.edit-profile-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(229, 62, 62, 0.4);
}

/* 统计数据样式 */
.user-stats {
  display: flex;
  justify-content: center;
  background-color: #fff;
  padding: 30px 0;
  border-bottom: 1px solid #f0f0f0;
  gap: 60px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: transform 0.3s ease;
  cursor: pointer;
}

.stat-item:hover {
  transform: translateY(-3px);
}

.stat-number {
  font-size: 28px;
  font-weight: 700;
  color: #e53e3e;
  margin-bottom: 5px;
}

.stat-label {
  font-size: 14px;
  color: #666;
}

/* 主内容区样式 */
  .profile-content {
    flex: 1;
    max-width: 1200px;
    margin: 30px auto;
    padding: 0 20px;
  }
  
  /* 两列布局容器 */
  .profile-columns {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 30px;
    margin-bottom: 30px;
  }
  
  .profile-section {
    background-color: #fff;
    border-radius: 16px;
    padding: 30px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    height: 100%;
  }
  
  /* 最近活动容器样式 */
  .recent-activities-container {
    grid-column: 1 / -1;
  }

.profile-section:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
}

.section-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 25px;
  padding-bottom: 15px;
  border-bottom: 2px solid #f5f5f5;
}

.section-header h2 {
  color: #333;
  font-size: 22px;
  font-weight: 600;
  margin: 0;
}

.section-icon {
  font-size: 20px;
}

/* 信息网格样式 */
.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 25px;
}

.info-item {
  display: flex;
  flex-direction: column;
  padding: 15px;
  background-color: #fafafa;
  border-radius: 12px;
  transition: background-color 0.3s ease;
}

.info-item:hover {
  background-color: #f5f5f5;
}

.info-item .label {
  color: #999;
  font-size: 14px;
  margin-bottom: 8px;
  font-weight: 500;
}

.info-item .value {
  color: #333;
  font-size: 16px;
  font-weight: 500;
}

  .info-item .value.bio {
  font-style: italic;
  line-height: 1.5;
}

.status-active {
  color: #4CAF50;
  font-weight: 600;
}

/* 安全信息样式 */
.security-info {
  display: flex;
  flex-direction: column;
  gap: 25px;
}

.security-actions {
  display: flex;
  gap: 15px;
  margin-top: 10px;
}

.home-btn {
  flex: 1;
  padding: 12px 24px;
  border: none;
  border-radius: 24px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  background: linear-gradient(135deg, #2196F3 0%, #0b7dda 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(33, 150, 243, 0.3);
}

.home-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(33, 150, 243, 0.4);
}

.security-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  background-color: #fafafa;
  border-radius: 12px;
  transition: background-color 0.3s ease;
}

.security-item:hover {
  background-color: #f5f5f5;
}

.security-left {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.security-label {
  color: #333;
  font-size: 16px;
  font-weight: 500;
}

.security-desc {
  color: #999;
  font-size: 14px;
}

.password-strength {
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 600;
}

.password-strength.strong {
  background-color: rgba(76, 175, 80, 0.1);
  color: #4CAF50;
}

.status-verified {
  color: #4CAF50;
  font-size: 20px;
  font-weight: bold;
}

.status-unverified {
  color: #ff9800;
  font-size: 14px;
  font-weight: 500;
}

.security-actions {
  display: flex;
  gap: 15px;
  margin-top: 10px;
}

.change-password-btn,
.logout-btn {
  flex: 1;
  padding: 12px 24px;
  border: none;
  border-radius: 24px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.change-password-btn {
  background: linear-gradient(135deg, #4caf50 0%, #45a049 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3);
}

.change-password-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(76, 175, 80, 0.4);
}

.logout-btn {
  background: linear-gradient(135deg, #f44336 0%, #d32f2f 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(244, 67, 54, 0.3);
}

.logout-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(244, 67, 54, 0.4);
}

/* 最近活动样式 */
.recent-activities {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.activity-item {
  display: flex;
  align-items: flex-start;
  gap: 15px;
  padding: 15px;
  background-color: #fafafa;
  border-radius: 12px;
  transition: all 0.3s ease;
}

.activity-item:hover {
  background-color: #f5f5f5;
  transform: translateX(5px);
}

.activity-icon {
  font-size: 24px;
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fff;
  border-radius: 50%;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.activity-content {
  flex: 1;
}

.activity-text {
  color: #333;
  font-size: 16px;
  margin-bottom: 5px;
}

.activity-time {
  color: #999;
  font-size: 14px;
}

/* 响应式设计 */
  @media (max-width: 1024px) {
    .profile-columns {
      grid-template-columns: 1fr;
      gap: 20px;
    }
  }
  
  @media (max-width: 768px) {
    .header-content {
      flex-direction: column;
      text-align: center;
      padding: 30px 20px;
    }
    
    .avatar-container {
      margin-right: 0;
      margin-bottom: 20px;
    }
    
    .user-name-section {
      flex-direction: column;
      gap: 10px;
    }
    
    .edit-profile-btn {
      margin-top: 20px;
    }
    
    .user-stats {
      flex-wrap: wrap;
      gap: 30px;
      padding: 20px 0;
    }
    
    .stat-item {
      min-width: 80px;
    }
    
    .stat-number {
      font-size: 24px;
    }
    
    .profile-content {
      padding: 0 15px;
      margin: 20px auto;
    }
    
    .profile-section {
      padding: 20px;
      margin-bottom: 20px;
    }
    
    .info-grid {
      grid-template-columns: 1fr;
      gap: 15px;
    }
    
    .security-actions {
      flex-direction: column;
    }
    
    .activity-item {
      padding: 12px;
    }

    .profile-actions {
      display: flex;
      gap: 15px;
    }

    .recharge-btn {
      padding: 12px 24px;
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      color: white;
      border: none;
      border-radius: 8px;
      font-size: 16px;
      font-weight: bold;
      cursor: pointer;
      transition: all 0.3s ease;
      text-decoration: none;
    }

    .recharge-btn:hover {
      transform: translateY(-2px);
      box-shadow: 0 6px 20px rgba(102, 126, 234, 0.3);
    }

    @media (max-width: 768px) {
      .profile-actions {
        flex-direction: column;
        width: 100%;
      }

      .recharge-btn {
        text-align: center;
      }
    }
  }
</style>